package ch.uzh.csg.samplepaymentproject;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.spongycastle.jce.ECNamedCurveTable;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.jce.spec.ECParameterSpec;
import android.util.Base64;
import ch.uzh.csg.mbps.customserialization.PKIAlgorithm;
import ch.uzh.csg.mbps.customserialization.exceptions.UnknownPKIAlgorithmException;
public class KeyHandler {
private static final String SECURITY_PROVIDER = "SC";
static {
Security.addProvider(new BouncyCastleProvider());
}
public static KeyPair generateKeyPair() throws UnknownPKIAlgorithmException, NoSuchAlgorithmException,
NoSuchProviderException, InvalidAlgorithmParameterException {
return generateKeyPair(PKIAlgorithm.DEFAULT);
}
public static KeyPair generateKeyPair(PKIAlgorithm algorithm) throws UnknownPKIAlgorithmException,
NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
if (algorithm.getCode() != PKIAlgorithm.DEFAULT.getCode())
throw new UnknownPKIAlgorithmException();
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(algorithm.getKeyPairSpecification());
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm.getKeyPairAlgorithm(), SECURITY_PROVIDER);
keyGen.initialize(ecSpec, new SecureRandom());
return keyGen.generateKeyPair();
}
public static PublicKey decodePublicKey(String publicKeyEncoded) throws UnknownPKIAlgorithmException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
return decodePublicKey(publicKeyEncoded, PKIAlgorithm.DEFAULT);
}
public static PublicKey decodePublicKey(String publicKeyEncoded, PKIAlgorithm algorithm) throws UnknownPKIAlgorithmException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
if (algorithm.getCode() != PKIAlgorithm.DEFAULT.getCode())
throw new UnknownPKIAlgorithmException();
byte[] decoded = Base64.decode(publicKeyEncoded.getBytes(), Base64.DEFAULT);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decoded);
KeyFactory keyFactory = KeyFactory.getInstance(algorithm.getKeyPairAlgorithm(), SECURITY_PROVIDER);
return keyFactory.generatePublic(publicKeySpec);
}
public static PrivateKey decodePrivateKey(String privateKeyEncoded) throws UnknownPKIAlgorithmException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
return decodePrivateKey(privateKeyEncoded, PKIAlgorithm.DEFAULT);
}
public static PrivateKey decodePrivateKey(String privateKeyEncoded, PKIAlgorithm algorithm) throws UnknownPKIAlgorithmException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
if (algorithm.getCode() != PKIAlgorithm.DEFAULT.getCode())
throw new UnknownPKIAlgorithmException();
byte[] decoded = Base64.decode(privateKeyEncoded.getBytes(), Base64.DEFAULT);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decoded);
KeyFactory keyFactory = KeyFactory.getInstance(algorithm.getKeyPairAlgorithm(), SECURITY_PROVIDER);
return keyFactory.generatePrivate(privateKeySpec);
}
}